package com.dayuanit.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayuanit.domain.Car;
import com.dayuanit.dto.AjaxResultDTO;
import com.dayuanit.exception.MallBusException;
import com.dayuanit.service.CarService;

@Controller
@RequestMapping("/car")
public class CarController extends BaseController {
	
	@Autowired
	private CarService carService;
	
	@RequestMapping("/addGood")
	@ResponseBody
	public AjaxResultDTO addGood(int goodId, HttpServletRequest request) {
		try {
			carService.addGood(goodId, getUserId(request));
			return AjaxResultDTO.success();
		} catch(MallBusException me) {
			return AjaxResultDTO.failed(me.getMessage());
		} catch (Exception e) {
			return AjaxResultDTO.failed("添加失败");
		}
		
	}
	
	@RequestMapping("/toCar")
	public String toCar(HttpServletRequest request) {
		
		List<Car> cars = carService.listCar(getUserId(request));
		request.setAttribute("carList", cars);
		return "cart";
	}
	
	@RequestMapping("/listCar")
	@ResponseBody
	public AjaxResultDTO listCar(HttpServletRequest request) {
		try {
			List<Car> cars = carService.listCar(getUserId(request));
			return AjaxResultDTO.success(cars);
		} catch(MallBusException me) {
			return AjaxResultDTO.failed(me.getMessage());
		} catch (Exception e) {
			return AjaxResultDTO.failed("添加失败");
		}
		
	}
	
	

}
